home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / ctlib100.zip / INSTALL.LZH / UTILS.PAS < prev   
Pascal/Delphi Source File  |  1996-10-12  |  17KB  |  464 lines

  1. {**************************************************************************}
  2. {*  BitSoft Development, L.L.C.                                           *}
  3. {*  Copyright (C) 1995, 1996 BitSoft Development, L.L.C.                  *}
  4. {*  All rights reserved.                                                  *}
  5. {**************************************************************************}
  6.  
  7. unit Utils;
  8.  
  9. {$X+}
  10.  
  11. interface
  12.  
  13. uses App, Objects, Views, Drivers, MsgBox,
  14.      BsdTest,
  15.      Containr,
  16.      Display, Types, Readers, Data;
  17.  
  18. var
  19.   TestRunning : Boolean;
  20.   { Indicates if a test is running }
  21.  
  22.   ExitTesting : Boolean;
  23.   { Indicates if testing has been aborted }
  24.  
  25.   NonStopTesting : Boolean;
  26.   { Indicates if no pauses must be made between sections of a test }
  27.  
  28.   TestWindow : PResultsWindow;
  29.   { Pointer to the window that is used to display the results of the test
  30.     currently running. }
  31.  
  32.   TestReader : PContainerReader;
  33.   { Pointer to the reader that is being used as the interface to the
  34.     data items in the container being currently tested. }
  35.  
  36. const
  37.   UseNonDynamicTestRec : Boolean = False;
  38.   { Tells Insert functions to create a non-dynamic test record instead of
  39.     using the standard CreateItem functions, which are used to dynamically
  40.     allocate data items. }
  41.  
  42.   UseNonDynamicTestObject : Boolean = False;
  43.   { Tells Insert functions to create a non-dynamic test object instead of
  44.     using the standard CreateItem functions, which are used to dynamically
  45.     allocate data items. }
  46.  
  47.   UseNonDynamicTestStaticObject : Boolean = False;
  48.   { Tells Insert functions to create a non-dynamic test static object
  49.     instead of using the standard CreateItem functions, which are used to
  50.     dynamically allocate data items. }
  51.  
  52.   TestingMemArray : Boolean = False;
  53.   { Constant used to determine if a memory array is being tested.  If this
  54.     is the case, when an Item in the array must be deleted, it will be freed
  55.     first. }
  56.  
  57. var
  58.   NonDynamicRec : TTestRec;
  59.   { Variable used in tests using non-dynamically allocated data. }
  60.  
  61.   NonDynamicObject : TTestObject;
  62.   { Variable used in tests using non-dynamically allocated data. }
  63.  
  64.   NonDynamicStaticObject : TTestStaticObject;
  65.   { Variable used in tests using non-dynamically allocated data. }
  66.  
  67. var
  68.   CreateItem : TCreateFunction;
  69.   { This function is used to construct the items that will be inserted
  70.     in the container being tested. }
  71.  
  72. function CanStartNewTest : Boolean;
  73. { Returns True if no other tests are running and therefore, a new test
  74.   can be started . }
  75.  
  76. procedure DisplayFooter;
  77. { Displayes a notice that the test has ended. }
  78.  
  79. procedure DisplayHeader;
  80. { Displays the instructions when starting a test. }
  81.  
  82. function DisplayMessage (AMessage:String): Boolean;
  83. { Displays a message at the bottom of the screen. }
  84.  
  85. procedure EndTest;
  86. { Displays a message indicating that the test has finished and the resets
  87.   the state of the application and of test variables.  This procedure
  88.   must always be called immediatly after any test. }
  89.  
  90. procedure EraseMessage;
  91. { Erases a message that was displayed using DisplayMessage. }
  92.  
  93. procedure InitTest(Reader: PContainerReader; Window: PResultsWindow;
  94.   CreateItemFunc : TCreateFunction);
  95. { Sets the value of several variables used throught tests and displays
  96.   the test's header.  This procedure must always be called immediatly
  97.   before starting any test. }
  98.  
  99. procedure NotifyDataChange;
  100. { Notifies the current scroller that the data has changed. }
  101.  
  102. procedure PauseTest;
  103. { Makes a pause and waits for user instructions. }
  104.  
  105. procedure ResetApplication;
  106. { Resets the state of the application after a test .}
  107.  
  108. procedure StartTest(TestHeader, TestSubHeader : string);
  109. { Diplays the headers for the next section of the test and starts the timer. }
  110.  
  111. procedure StopTest;
  112. { Stops the timer and displays the time elapsed. }
  113.  
  114. procedure WriteHeader(TestHeader : string);
  115. { Displays the header of the test. }
  116.  
  117. procedure WriteNumResult(ResultString: string; Result: LongInt);
  118. { Displays a subheader with a numeric result. }
  119.  
  120. procedure WriteResult(ResultString: string);
  121. { Displays a subheader with a string result. }
  122.  
  123. procedure WriteSubHeader(TestSubHeader : string);
  124. { Displays the subheader (one line description) for the next section of the
  125.   test. }
  126.  
  127. procedure WriteTime;
  128. { Displays the time that the last time took to complete. }
  129.  
  130. type
  131.   PMessageLine = ^TMessageLine;
  132.   TMessageLine = object(TView)
  133.   { Displays the string stored in the StatusMessage attribute.  This object
  134.     is used to display status line messages }
  135.       StatusMessage : String[79];
  136.     constructor Init (Bounds:TRect; AMessage:String);
  137.     procedure Draw; virtual;
  138.   end; { TMessageLine }
  139.  
  140. var
  141.   MessageLine : PMessageLine;
  142.   { Global variable used to display messages at the bottom of the screen }
  143.  
  144. implementation
  145.  
  146. {****************************************************************************}
  147. { TMessageLine object                                                        }
  148. {****************************************************************************}
  149. {****************************************************************************}
  150. { TMessageLine.Init                                                          }
  151. {****************************************************************************}
  152. constructor TMessageLine.Init(Bounds:TRect; AMessage:String);
  153. begin
  154.   TView.Init(Bounds);
  155.   StatusMessage := ' '+AMessage;
  156. end;
  157.  
  158. {****************************************************************************}
  159. { TMessageLine.Draw                                                          }
  160. {****************************************************************************}
  161. procedure TMessageLine.Draw;
  162. var
  163.   B : TDrawBuffer;
  164.   C : Byte;
  165. begin
  166.   C := GetColor(2);
  167.   MoveChar(B, ' ', C, Size.X);
  168.   MoveStr(B, StatusMessage, C);
  169.   WriteLine(0, 0, Size.X, 1, B);
  170. end;
  171.  
  172. {****************************************************************************}
  173. { CanStartNewTest                                                            }
  174. {****************************************************************************}
  175. function CanStartNewTest : Boolean;
  176. begin
  177.   if not TestRunning
  178.     then CanStartNewTest := True
  179.     else begin
  180.            MessageBox('Please finish the current test before testing '+
  181.              'another object.', nil, mfWarning + mfOkButton);
  182.            CanStartNewTest := False;
  183.          end; { else }
  184. end;
  185.  
  186. {****************************************************************************}
  187. { DisplayFooter                                                              }
  188. {****************************************************************************}
  189. procedure DisplayFooter;
  190. begin
  191.   with TestWindow^ do
  192.   begin
  193.     Writeln(T);
  194.     Writeln(T, '-------------------------------------------------------------');
  195.     Writeln(T, 'Done testing the object.  No errors ocurred.');
  196.     Writeln(T);
  197.     Writeln(T, 'Note: if many items were created and the program is running');
  198.     Writeln(T, 'in real mode, it may take a while after closing the window,');
  199.     Writeln(T, 'before all items in the container get disposed of.');
  200.     Writeln(T);
  201.     Writeln(T, 'End of test.');
  202.   end; { with }
  203. end;
  204.  
  205. {****************************************************************************}
  206. { DisplayHeader                                                              }
  207. {****************************************************************************}
  208. procedure DisplayHeader;
  209. begin
  210.   with TestWindow^ do
  211.   begin
  212.     Writeln(T, 'After each step in the test, please press (N) to go to the');
  213.     Writeln(T, 'next test, (C) for continuous testing, or (X) to cancel.');
  214.     Writeln(T);
  215.     Writeln(T, 'Press (N) or (C) now to start testing.');
  216.     Writeln(T, '-------------------------------------------------------------');
  217.     Writeln(T, '');
  218.   end; { with }
  219. end;
  220.  
  221. {****************************************************************************}
  222. { DisplayMessage                                                             }
  223. {****************************************************************************}
  224. function DisplayMessage (AMessage : String) : Boolean;
  225. var
  226.   R : TRect;
  227. begin
  228.   DisplayMessage := False;
  229.   Application^.GetExtent(R);
  230.   R.A.Y := R.B.Y - 1;
  231.   if MessageLine <> NIL then
  232.     begin
  233.       MessageLine^.StatusMessage := ' ' + AMessage;
  234.       MessageLine^.Draw;
  235.     end {...if MessageLine <> NIL }
  236.   else
  237.     begin
  238.       MessageLine := New(PMessageLine, Init(R, AMessage));
  239.       if MessageLine^.Valid(cmValid) = True then
  240.         begin
  241.           Application^.Insert(MessageLine);
  242.           DisplayMessage := True;
  243.         end {...if MessageLine^.Valid(cmValid) = True }
  244.       else
  245.         MessageLine := NIL;
  246.     end; {...if/else }
  247. end;
  248.  
  249. {****************************************************************************}
  250. { EndTest                                                                    }
  251. {****************************************************************************}
  252. procedure EndTest;
  253. begin
  254.   if not ExitTesting
  255.     then begin
  256.            DisplayFooter;
  257.            ResetApplication;
  258.          end; { if }
  259. end;
  260.  
  261. {****************************************************************************}
  262. { EraseMessage                                                               }
  263. {****************************************************************************}
  264. procedure EraseMessage;
  265. begin
  266.   if MessageLine <> nil
  267.     then Dispose(MessageLine , Done);
  268.   MessageLine := nil;
  269. end;
  270.  
  271. {****************************************************************************}
  272. { InitTest                                                                   }
  273. {****************************************************************************}
  274. procedure InitTest(Reader: PContainerReader; Window: PResultsWindow;
  275.   CreateItemFunc : TCreateFunction);
  276. var
  277.   OldTitle : string;
  278. begin
  279.   TestRunning := True;
  280.   ExitTesting := False;
  281.   NonStopTesting := False;
  282.   TestReader := Reader;
  283.   TestWindow := Window;
  284.   CreateItem := CreateItemFunc;
  285.   with TestWindow^ do
  286.   begin
  287.     OldTitle := Title^;
  288.     DisposeStr(Title);
  289.     Title := NewStr(OldTitle + ' (testing)');
  290.   end; { with }
  291.   Desktop^.Insert(TestWindow);
  292.   DisplayHeader;
  293.   PauseTest;
  294. end;
  295.  
  296. {****************************************************************************}
  297. { NotifyDataChanged                                                          }
  298. {****************************************************************************}
  299. procedure NotifyDataChange;
  300. begin
  301.   TestWindow^.Scroller^.Reader^.HasChanged := True;
  302. end;
  303.  
  304. {****************************************************************************}
  305. { Pause                                                                      }
  306. {****************************************************************************}
  307. procedure PauseTest;
  308. var
  309.   Event : TEvent;
  310. begin
  311.   TestWindow^.Redraw;
  312.   if NonStopTesting
  313.     then begin
  314.            Application^.Idle;
  315.            Exit;
  316.          end; { if }
  317.   Application^.GetEvent(Event);
  318.   repeat
  319.     Application^.HandleEvent(Event);
  320.     Application^.Idle;
  321.     Application^.GetEvent(Event);
  322.   until ((Event.What = evKeyDown) and
  323.         (UpCase(Event.CharCode) in ['C', 'X', 'N'])) or
  324.         ((Event.What = evCommand) and ((Event.Command = cmClose) or
  325.         (Event.Command = cmQuit)));
  326.   if Desktop^.Current = PView(TestWindow)
  327.     then if ((Event.What = evCommand) and ((Event.Command = cmClose) or
  328.             (Event.Command = cmQuit)))
  329.            then begin
  330.                   Writeln(TestWindow^.T);
  331.                   Writeln(TestWIndow^.T);
  332.                   Writeln(TestWindow^.T, 'Test aborted...');
  333.                   ExitTesting := True;
  334.                   ResetApplication;
  335.                   Application^.HandleEvent(Event);
  336.                 end { case of }
  337.            else case UpCase(Event.CharCode) of
  338.                   'X' : begin
  339.                           Writeln(TestWindow^.T);
  340.                           Writeln(TestWIndow^.T);
  341.                           Writeln(TestWindow^.T, 'Test aborted...');
  342.                           ExitTesting := True;
  343.                           ResetApplication;
  344.                         end; { case of }
  345.                   'C' : NonStopTesting := True;
  346.                 end { case of }
  347.     else if (Event.What = evCommand) and (Event.Command = cmQuit)
  348.            then Desktop^.Current := TestWindow
  349.            else if Event.What = evKeyDown
  350.                   then begin
  351.                          MessageBox('Please select the current test window '+
  352.                            'before continuing.', nil, mfError + mfOkButton);
  353.                          Pause;
  354.                        end { if }
  355.                   else begin
  356.                          MessageBox('Please close the current test window, '+
  357.                            'before continuing.', nil, mfError + mfOkButton);
  358.                          PauseTest;
  359.                        end; { else }
  360. end;
  361.  
  362. {****************************************************************************}
  363. { ResetApplication                                                           }
  364. {****************************************************************************}
  365. procedure ResetApplication;
  366. var
  367.   OldTitle : string;
  368. begin
  369.   with TestWindow^ do
  370.   begin
  371.     OldTitle := Title^;
  372.     DisposeStr(Title);
  373.     Title := NewStr(Copy(OldTitle, 1, Length(OldTitle) -10));
  374.   end;
  375.   TestWindow^.ReDraw;
  376.   TestRunning := False;
  377. end;
  378.  
  379. {****************************************************************************}
  380. { StartTest                                                                  }
  381. {****************************************************************************}
  382. procedure StartTest(TestHeader, TestSubHeader : string);
  383. begin
  384.   WriteHeader(TestHeader);
  385.   WriteSubHeader(TestSubHeader);
  386.   SetInitTime;
  387. end;
  388.  
  389. {****************************************************************************}
  390. { StopTest                                                                   }
  391. {****************************************************************************}
  392. procedure StopTest;
  393. begin
  394.   SetFinalTime;
  395.   WriteTime;
  396. end;
  397.  
  398. {****************************************************************************}
  399. { WriteHeader                                                                }
  400. {****************************************************************************}
  401. procedure WriteHeader(TestHeader : string);
  402. begin
  403.   with TestWindow^ do
  404.   begin
  405.     writeln(T);
  406.     writeln(T, 'Testing : ', TestHeader);
  407.     writeln(T);
  408.   end; { with }
  409. end;
  410.  
  411. {****************************************************************************}
  412. { WriteNumResult                                                             }
  413. {****************************************************************************}
  414. procedure WriteNumResult(ResultString: string; Result: LongInt);
  415. begin
  416.   WriteSubHeader(ResultString);
  417.   Writeln(TestWindow^.T, Result:13);
  418. end;
  419.  
  420. {****************************************************************************}
  421. { WriteResult(var                                                            }
  422. {****************************************************************************}
  423. procedure WriteResult(ResultString: string);
  424. begin
  425.   WriteSubHeader('Result:');
  426.   Writeln(TestWindow^.T, ResultString:13);
  427. end;
  428.  
  429. {****************************************************************************}
  430. { WriteSubHeader                                                             }
  431. {****************************************************************************}
  432. procedure WriteSubHeader(TestSubHeader : string);
  433. var
  434.   S : string;
  435.   P : Integer;
  436. const
  437.   MaxLineSize = 48;
  438. begin
  439.   if Length(TestSubHeader) > MaxLineSize
  440.     then begin
  441.            S := Copy(TestSubHeader, 1, MaxLineSize);
  442.            P := MaxLineSize;
  443.            while S[P] <> ' ' do
  444.              Dec(P);
  445.            S := Copy(TestSubHeader, 1, P);
  446.            writeln(TestWindow^.T, S:48);
  447.            TestSubHeader := Copy(TestSubHeader, Succ(P),
  448.              Succ(Length(TestSubHeader) - Succ(P)));
  449.          end; { if }
  450.   write(TestWindow^.T, TestSubHeader:48);
  451. end;
  452.  
  453. {****************************************************************************}
  454. { WriteTime                                                                  }
  455. {****************************************************************************}
  456. procedure WriteTime;
  457. begin
  458.   writeln(TestWindow^.T, CalculateTime:13);
  459. end;
  460.  
  461. begin
  462.   TestRunning := False;
  463.   CreateItem := nil;
  464. end.